package db.cn.edu.scau.cmi.huxiantao.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import db.cn.edu.scau.cmi.huxiantao.domain.Teacher;
import db.cn.edu.scau.cmi.huxiantao.util.SessionFactoryUtil;

public class TeacherDAO {
	private static final Logger log = LoggerFactory.getLogger(TeacherDAO.class);
	
	public boolean save(Teacher teacher) {
		log.debug("saving Teacher instance");
		
		Session session=SessionFactoryUtil.getSession();
		Transaction tx=session.beginTransaction();
		
		try {
			session.save(teacher);
			tx.commit();
			log.debug("save successfully");
			return true;
		} catch (RuntimeException e) {
			log.error("save failed", e);
			tx.rollback();
			e.printStackTrace();
			return false;
		} finally {
			session.close();
		}
	}
	
	public boolean delete(Teacher teacher) {
		log.debug("deleting Teacher instance");
		Session session=SessionFactoryUtil.getSession();
		Transaction tx=session.beginTransaction();
		
		try {
			session.delete(teacher);
			tx.commit();
			log.debug("delete successfully");
			return true;
		}catch (RuntimeException e) {
			log.error("delete failed",e);
			tx.rollback();
			e.printStackTrace();
			return false;
		}finally {
			session.close();
		}
	}
	
	public boolean update(Teacher teacher) {
		log.debug("updating Teacher instance");
		Session session=SessionFactoryUtil.getSession();
		Transaction tx=session.beginTransaction();
		
		try {
			session.update(teacher);
			tx.commit();
			log.debug("update successfully");
			return true;
		}catch(RuntimeException e) {
			log.error("update failed",e);
			tx.rollback();
			e.printStackTrace();
			return false;
		}finally {
			session.close();
		}
	}
	
	public Teacher findById(int id) {
		log.debug("finding Teacher instance by id");
		Session session=SessionFactoryUtil.getSession();
		Transaction tx=session.beginTransaction();
		Teacher teacher=null;
		
		try {
			teacher=session.get(Teacher.class, id);
			tx.commit();
			log.debug("findById successfully");
		}catch(RuntimeException e) {
			log.error("findById failed",e);
			tx.rollback();
			e.printStackTrace();
		}finally {
			session.close();
		}
		return teacher;
	}
	
	public List<Teacher> findByProperty(String propertyName,Object value) {
		log.debug("finding Teacher instance with property: {} , value: {} ",propertyName,value);
		Session session=SessionFactoryUtil.getSession();
		Transaction tx=session.beginTransaction();
		List<Teacher> teachers=null;
		
		try {
			String queryString="from Teacher as model where model." + propertyName + "= ?0";
			Query<Teacher> query=session.createQuery(queryString);
			query.setParameter(0, value);
			teachers=query.list();
			tx.commit();
			log.debug("findByProperty successfully");
		}catch(RuntimeException e) {
			log.error("findByProperty failed",e);
			tx.rollback();
			e.printStackTrace();
		}finally {
			session.close();
		}
		return teachers;
	}
	
	public List<Teacher> findAll() {
		log.debug("finding all teachers");
		Session session=SessionFactoryUtil.getSession();
		Transaction tx=session.beginTransaction();
		List<Teacher> teachers=null;
		
		try {
			teachers=session.createQuery("from Teacher").list();
			tx.commit();
			log.debug("findAll successfully");
		}catch(RuntimeException e) {
			log.error("findAll failed",e);
			tx.rollback();
			e.printStackTrace();
		}finally {
			session.close();
		}
		return teachers;
	}
}
